﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.DAO
{
    public class Coupon_DAO
    {
        private static QL_DoanhNghiepDataContext data = new QL_DoanhNghiepDataContext();

        public static List<COUPON> ListCouponByElectricDeviceID(string ElectricDeviceID)
        {
            List<COUPON> listCoupon = new List<COUPON>();

            var list = from cp in data.COUPONs
                       where cp.ElectricDevice_ID == ElectricDeviceID
                       select new
                       {
                           cp.Coupon_ID,
                           cp.TimeToEndSelling,
                           cp.TimeToStartSelling,
                           cp.TimeToStartUsing,
                           cp.TimeToEndUsing,
                           cp.Status,
                           cp.QuantityForDiscountPrice,
                       };

            foreach (var item in list)
            {
                COUPON cp = new COUPON();
                cp.Coupon_ID = item.Coupon_ID;
                cp.TimeToStartSelling = item.TimeToStartSelling;
                cp.TimeToEndSelling = item.TimeToEndSelling;
                cp.TimeToStartUsing = item.TimeToStartUsing;
                cp.TimeToEndUsing = item.TimeToEndUsing;
                cp.Status = item.Status;
                cp.QuantityForDiscountPrice = item.QuantityForDiscountPrice;
                listCoupon.Add(cp);
            }
            return listCoupon;
        }

        public static void UpdateStatusCoupon(string Coupon_ID, int Status)
        {
            data.spUpdateStatusCoupon(Coupon_ID, Status);
        }

        public static COUPON DetailCoupon(string Coupon_ID)
        {
            COUPON coupon = new COUPON();
            var list = from cp in data.COUPONs
                       where cp.Coupon_ID == Coupon_ID
                       select new
                       {
                           cp.Coupon_ID,
                           cp.NonDiscountPrice,
                           cp.DiscountPrice,
                           cp.Date,
                           cp.TimeToEndSelling,
                           cp.TimeToEndUsing,
                           cp.TimeToStartSelling,
                           cp.TimeToStartUsing,
                           cp.QuantityForDiscountPrice,
                           cp.Description,
                           cp.UsingCondition,
                       };
            foreach (var item in list)
            {
                coupon.Coupon_ID = item.Coupon_ID;
                coupon.NonDiscountPrice = item.NonDiscountPrice;
                coupon.DiscountPrice = item.DiscountPrice;
                coupon.Date = item.Date;
                coupon.TimeToStartSelling = item.TimeToStartSelling;
                coupon.TimeToEndSelling = item.TimeToEndSelling;
                coupon.TimeToStartUsing = item.TimeToStartUsing;
                coupon.TimeToEndUsing = item.TimeToEndUsing;
                coupon.QuantityForDiscountPrice = item.QuantityForDiscountPrice;
                coupon.Description = item.Description;
                coupon.UsingCondition = item.UsingCondition;
            }
            return coupon;
        }

        public static void UpdateCoupon(COUPON coupon)
        {
            data.spUpdateCoupon(coupon.Coupon_ID, coupon.Date, coupon.NonDiscountPrice, coupon.DiscountPrice, coupon.TimeToStartSelling, coupon.TimeToEndSelling, coupon.TimeToStartUsing, coupon.TimeToEndUsing, coupon.QuantityForDiscountPrice, coupon.Description, coupon.UsingCondition);
        }

        public static void InsertCoupon(COUPON coupon)
        {
            var list = from cp in data.COUPONs
                       select cp;      
            coupon.Coupon_ID = "CP000" + (list.Count() + 1).ToString();
            data.spInsertCoupon(coupon.Coupon_ID, coupon.ElectricDevice_ID, coupon.NonDiscountPrice, coupon.DiscountPrice, coupon.Date, coupon.TimeToStartSelling, coupon.TimeToEndSelling, coupon.TimeToStartUsing, coupon.TimeToEndUsing, coupon.QuantityForDiscountPrice, coupon.Description, coupon.UsingCondition);
        }

        public static void GetPriceCouponOfElectricDevice(string ElectricDevice_ID, ref string NonDiscountPrice, ref string DiscountPrice)
        {
            var list = from cp in data.COUPONs
                       where cp.ElectricDevice_ID == ElectricDevice_ID && cp.Status == 1
                       orderby cp.TimeToStartSelling descending
                       select new
                       {
                           cp.NonDiscountPrice,
                           cp.DiscountPrice,
                       };
            var temp = list.ToList();
            if (temp.Count > 0)
            {
                NonDiscountPrice = float.Parse(temp[0].NonDiscountPrice).ToString("###,###,###");
                DiscountPrice = float.Parse(temp[0].DiscountPrice).ToString("###,###,###");
            }
        }

        public static List<OrderCoupon> GetAllOrderCoupon()
        {
            List<OrderCoupon> orderCoupons = new List<OrderCoupon>();
            try
            {
                //truy van CSDL
                var result = from o in data.ORDER_COUPONs
                             join c in data.CUSTOMERs on o.UserID equals c.UserID
                             select new
                             {
                                 o.OrderCouponID,
                                 c.Name_Company,
                                 o.OrderedDate,
                                 o.OrderTransportDate,
                                 o.PaymentSatus,
                                 o.TransportStatus,
                                 o.status,
                                 o.TransportType
                             };
                foreach (var item in result)
                {
                    OrderCoupon orderCoupon = new OrderCoupon();
                    orderCoupon.OrderCouponID = item.OrderCouponID;
                    orderCoupon.Name_Company = item.Name_Company;
                    orderCoupon.OrderedDate = (DateTime)item.OrderedDate;
                    //if (item.OrderTransportDate != null)
                    //    orderCoupon.OrderTransportDate = (DateTime)item.OrderTransportDate;

                    orderCoupon.PaymentSatus = (short)item.PaymentSatus;
                    orderCoupon.TransportStatus = (short)item.TransportStatus;
                    orderCoupon.Status = (short)item.status;
                    orderCoupon.TransportType_Name = item.TransportType;

                    orderCoupons.Add(orderCoupon);
                }

                return orderCoupons;
            }
            catch
            {
                return null;
            }

        }

        public static bool UpdateStatusColumnOrderCoupon(string orderCouponID, short status)
        {
            try
            {
                //
                ORDER_COUPON orderCoupon = data.ORDER_COUPONs.Single(o => o.OrderCouponID == orderCouponID);
                orderCoupon.status = status;

                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
